Sorting numbers in descending order means arranging them from the largest to the smallest.
def sort_numbers_descending(numbers):
return sorted(numbers, reverse=True)
# Taking input for numbers and sorting them in descending order
def sort_and_display_numbers_descending():
numbers = [int(x) for x in input("Enter numbers separated by spaces: ").split()]
sorted_numbers_descending = sort_numbers_descending(numbers)
print("Numbers sorted in descending order:", sorted_numbers_descending)
sort_and_display_numbers_descending()
Enter numbers separated by spaces: 5 2 8 1 4
Numbers sorted in descending order: [8, 5, 4, 2, 1]
Enter numbers separated by spaces: 10 3 7 9 2
Numbers sorted in descending order: [10, 9, 7, 3, 2]
The function sort_numbers_descending(numbers)
sorts a list of numbers in descending order using the built-in sorted()
function with the reverse=True
argument.
The function sort_and_display_numbers_descending()
takes input for numbers, sorts them in descending order using the aforementioned function, and prints the sorted numbers.